package com.adv.ems.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.adv.ems.db.mapper.EnergyDataCumulateReportHourMapper;
import com.adv.ems.db.entity.EnergyDataCumulateReportHourDO;
import com.adv.ems.manager.EnergyDataCumulateReportHourManager;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Service
public class EnergyDataCumulateReportHourManagerImpl extends ServiceImpl<EnergyDataCumulateReportHourMapper, EnergyDataCumulateReportHourDO> implements EnergyDataCumulateReportHourManager {
    @Override
    public void deleteByPointColDate(Long pointId, Date hour) {
        LambdaUpdateWrapper<EnergyDataCumulateReportHourDO> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(EnergyDataCumulateReportHourDO::getCollectPointId, pointId);
        wrapper.eq(EnergyDataCumulateReportHourDO::getColDate, hour);
        remove(wrapper);
    }

    @Override
    public void insertData(EnergyDataCumulateReportHourDO reportHour) {
        save(reportHour);
    }

    @Override
    public List<EnergyDataCumulateReportHourDO> getAllDataByPonitTime(Long pointId, Date start, Date end) {
        LambdaUpdateWrapper<EnergyDataCumulateReportHourDO> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(EnergyDataCumulateReportHourDO::getCollectPointId, pointId);
        wrapper.ge(EnergyDataCumulateReportHourDO::getColDate, start);
        wrapper.lt(EnergyDataCumulateReportHourDO::getColDate, end);
        wrapper.orderByAsc(EnergyDataCumulateReportHourDO::getColDate);
        return list(wrapper);
    }

    @Override
    public List<EnergyDataCumulateReportHourDO> getDataByPointIdsTime(Set<Long> realPointIds, Date time) {
        LambdaUpdateWrapper<EnergyDataCumulateReportHourDO> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(EnergyDataCumulateReportHourDO::getCollectPointId, realPointIds);
        wrapper.eq(EnergyDataCumulateReportHourDO::getColDate, time);
        return list(wrapper);
    }

    @Override
    public List<EnergyDataCumulateReportHourDO> selectData(Date startDate, Date endDate, List<Long> pointIds) {
        LambdaQueryWrapper<EnergyDataCumulateReportHourDO> wrapper = new LambdaQueryWrapper<>();
        if (CollectionUtils.isEmpty(pointIds)) {
            return new ArrayList<>();
        }
        wrapper.in(EnergyDataCumulateReportHourDO::getCollectPointId, pointIds);
        wrapper.ge(EnergyDataCumulateReportHourDO::getColDate, startDate);
        wrapper.le(EnergyDataCumulateReportHourDO::getColDate, endDate);
        return list(wrapper);
    }
}
